home
diamond Go Premium
Data Engineering Path  ·  PySpark

RDD - Transformation Filter

The filter() transformation evaluates a user-defined boolean condition on each element of an existing RDD. It returns a new RDD containing only the elements that return True for that condition.


Key Characteristics

  • Subset Selection: The output RDD contains a subset of the parent RDD's records. If all elements fail the condition, the child RDD will be completely empty.
  • Narrow Dependency: Like map(), filter() processes elements locally within their existing partitions. No global network shuffle is required.
  • Preserves Structures: The output elements retain their original data types and forms.

PySpark Code Examples

Setup Spark Session

from pyspark.sql import SparkSession

spark = SparkSession.builder \
    .appName("RDD Transformation Filter") \
    .master("local[*]") \
    .getOrCreate()

sc = spark.sparkContext

Example A: Filtering Numeric Values

Let's filter out odd numbers and keep only the even numbers from a list:

# 1. Create a raw RDD
numbers = sc.parallelize([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# 2. Filter for even values (lazy)
evens = numbers.filter(lambda x: x % 2 == 0)

# 3. Trigger action
print("Even Numbers:", evens.collect())
# Output: Even Numbers: [2, 4, 6, 8, 10]

Example B: Extracting Error Lines from Logs

Let's filter raw logging strings to isolate only ERROR state lines:

# 1. Logs RDD
logs = sc.parallelize([
    "INFO: User login successful",
    "WARN: Low disk space detected",
    "ERROR: Connection timed out",
    "INFO: Database index queried",
    "ERROR: Write operation failed"
])

# 2. Filter lines that start with 'ERROR'
errors = logs.filter(lambda log: log.startswith("ERROR"))

# 3. Fetch results
print("Isolated Errors:")
for err in errors.collect():
    print(f"  {err}")

# Expected Output:
# Isolated Errors:
# ERROR: Connection timed out
# ERROR: Write operation failed

Example C: Complex Conditions

Let's filter a collection of users to select only those who are active AND aged 25 or older:

# 1. RDD of user tuples: (Username, Age, IsActive)
users = sc.parallelize([
    ("Alice", 28, True),
    ("Bob", 19, True),
    ("Charlie", 32, False),
    ("David", 24, True),
    ("Eve", 26, True)
])

# 2. Filter with compound lambda condition
active_adults = users.filter(lambda user: user[1] >= 25 and user[2] is True)

print("Active Adults (>=25):", active_adults.collect())
# Output: Active Adults (>=25): [('Alice', 28, True), ('Eve', 26, True)]
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.